//Heimild: http://guruparan.blog.com/2012/04/13/connecting-android-to-sql-server-via-wcf-using-json-part-2/

package com.stadsetjari;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Login extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    
	//Property declaration
	Button btnLogin;
	TextView lblStatus;
	EditText txtUserName,txtPassword;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnLogin=(Button)findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(this);
        
        lblStatus=(TextView)findViewById(R.id.lblStatus);
        
        txtUserName=(EditText)findViewById(R.id.txtUserName);
        txtPassword=(EditText)findViewById(R.id.txtPassword);
    }

	
	public void onClick(View v) {
		
		switch(v.getId())
		{
			case R.id.btnLogin:
				String userName=txtUserName.getText().toString();
				Log.d("username",txtUserName.getText().toString());
				Log.d("username",txtPassword.getText().toString());
				String password=txtPassword.getText().toString();
				if(verifyLogin(userName,password))
				{
					lblStatus.setText("Login Successful");
					
				}
				else
				{
					lblStatus.setText("Login Failed");
				}    
				break;
		}
	}
	
	public static String convertStreamToString(InputStream is) 
    {
    	BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    	StringBuilder sb = new StringBuilder();
    	
    	String line = null;
    	try {
    		while ((line = reader.readLine()) != null) {
    			sb.append(line + "\n");
    		}
    	} 
    	catch (IOException e) {
    		e.printStackTrace();
    	} 
    	finally {
    		try {
    			is.close();
    		} catch (IOException e) {
				e.printStackTrace();
    		}
    	}
    	return sb.toString();
    }
    
    public static boolean verifyLogin(String UserName,String Password)
    {
    	try
    	{
    		    	
    		DefaultHttpClient httpClient=new DefaultHttpClient(); 
    		String link = "http://stadsetjariandroid.azurewebsites.net/Service1.svc/checkLogin?name="+UserName+"&pass="+Password;
    		Log.d("url",link);
    		//Connect to the server
    		HttpGet httpGet=new HttpGet(link); 
    		//Get the response
    		HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
        	InputStream stream=httpEntity.getContent();
        	//Convert the stream to readable format
            String result= convertStreamToString(stream);
           
            if(result.charAt(1)=='1')
            {
            	return true;
            }
            else
            {
            	return false;
            }
    	}
    	catch(Exception e)
    	{
    		Log.d("ERROR",e.getMessage());
    		return false;
    	}
    	
    	
    }
	
	
}